﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;

namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class UpdateProductionReportCommandHanders : IRequestHandler<UpdateProductionReportCommands, APIResult<ProductionReport>>
    {
        private readonly IBaseRepository<ProductionReport> baseRepository;
        private readonly IMapper mapper;

        public UpdateProductionReportCommandHanders(IBaseRepository<ProductionReport> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 修改生产报工
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<ProductionReport>> Handle(UpdateProductionReportCommands request, CancellationToken cancellationToken)
        {
            APIResult<ProductionReport> result=new APIResult<ProductionReport> ();
            result.Code = APIEnum.修改成功;
            result.Msg = "修改成功";
            var map = mapper.Map<ProductionReport>(request);
            var list = $"Update ProductionReport set ReportTypeId='{request.ReportTypeId}',WorkStation='{request.WorkStation}',ProductionId='{request.ProductionId}',ProductMaterialCode='{request.ProductMaterialCode}',ProductMaterialName='{request.ProductMaterialName}',SpecificationModel='{request.SpecificationModel}',QuantityReported='{request.QuantityReported}',Caller='{request.Caller}',CallTime=@CallTime,Auditor='{request.Auditor}',Status='{request.Status}',ProductionTaskNumber='{request.ProductionTaskNumber}',Unit='{request.Unit}',Remark='{request.Remark}',IsDelete=@IsDelete where ProductionReportId='{request.ProductionReportId}'";
            await baseRepository.Update(map, list);
            return result;
        }
    }
}
